#!/bin/sh
DEVICE="/dev/ttyS0"
LOG="/tmp/26x16.log"
stty --f $DEVICE raw -cstopb -crtscts -parenb -parodd cs8 19200 -onlcr -echo
TMP=""
#echo -ne '\376\107\005\002' > $DEVICE   # Goto(5,2):  254 71 5 2 

clearsrc ( )
{
	dtoo 254
	arg0=$TMP
	dtoo 88
	arg1=$TMP
	echo -n '\376\130' > $DEVICE           # Clear screen: 254 88
	#echo -n '\376\130' > $DEVICE           # Clear screen: 254 88
	echo -n '\376\107\000\000' > $DEVICE  
}
dtoo ( )
{
	TMP=$(echo "obase=8;($1)" | bc)
}
gotoxy ( )
{
	echo '\376\107\$1\$2' > $DEVICE  
}

nextline ( )
{
	echo -n '\15\12' > $DEVICE  
}


#==========WIDGETS==========#
showdate ( )
{
	DATE=$(date "+%a %b %d %T")
	echo -n "  $DATE" > $DEVICE
}

cpu ( )
{
#18:22:38     all    6,03    0,03    1,37    0,29    0,02    0,12    0,00   92,15    228,62
#	CPU0=$(cat /tmp/cpu0.tmp| cut -c20-25)
#	echo  -n "CPU0 = $CPU0" >>$DEVICE
#	nextline
#	CPU1=$(cat /tmp/cpu1.tmp| cut -c20-25)
#	echo  "CPU1 = $CPU1" >> $DEVICE
	echo -n "(0)$(cat /proc/cpuinfo | grep MHz | head -n1)" > $DEVICE
	nextline	
#echo -n '\12' > $DEVICE 
	echo -n "(1)$(cat /proc/cpuinfo | grep MHz | tail -n1)" > $DEVICE
}

meminfo ( )
{
	TOTALMEM=$(cat /proc/meminfo | grep MemTotal | cut -f2 -d':'| tr -d " kB")
	FREEMEM=$(cat /proc/meminfo | grep MemFree | cut -f2 -d':'| tr -d " kB")
	USEDMEM=$(expr $TOTALMEM - $FREEMEM)
	PERCENT=$(echo "$USEDMEM * 100 / $TOTALMEM" | bc)
	echo -n "M.TOTAL: $TOTALMEM kB" > $DEVICE
	nextline
	echo -n "M.USADA: $USEDMEM kB $PERCENT%" > $DEVICE
	nextline
	PBAR=$(echo "($PERCENT*26)/100" | bc)

	while [ $PBAR -gt 0 ]
	do
		echo -n "#" > $DEVICE
		PBAR=$(($PBAR-1))
	done


#	echo -n "$PBAR"  > $DEVICE

}
ptop ( )
{
	echo -n "cpu mem proc" > $DEVICE
	nextline
	echo -n $(ps axo pcpu,pmem,comm --sort=pcpu| tail -n 3 | head -n 1) > $DEVICE
	nextline
	echo -n $(ps axo pcpu,pmem,comm --sort=pcpu| tail -n 2 | head -n 1) > $DEVICE
	nextline
	echo -n $(ps axo pcpu,pmem,comm --sort=pcpu| tail -n 1 | head -n 1) > $DEVICE
}
#Main loop

while [ true ]
do
#	mpstat -P 0 1 1| tail -n 1 | cut -c20-25 > /tmp/cpu0.tmp &
#	mpstat -P 1 1 1| tail -n 1 | cut -c20-25 > /tmp/cpu1.tmp &
	clearsrc
	showdate	
	nextline
	meminfo
	nextline
	cpu
	nextline
	ptop	 
	sleep 1 
done
